﻿// See https://aka.ms/new-console-template for more information
using ParallelEx;
using System.Diagnostics;

Console.WriteLine("Hello, World!");


//Mock 5000 Datas
var datas = new List<string>();
for (int i = 0; i < 5000; i++)
{
    datas.Add(i.ToString());
}


var threadCount = 20;
var batchSize = 50;

var locker = new object();

var sw = new Stopwatch();
sw.Start();

var result = ParallelHelper.ForEach(datas, batchSize, threadCount, (idx, data, dic) =>
{
    //YOUR LOGIC
    Thread.Sleep(1000);

    lock (locker)
    {
        dic.Remove(idx);
        Console.WriteLine($"{DateTime.Now} IDX:{idx},Data:{data.First()}, 剩余批次:{dic.Count}");
    }
});

if (result.IsCompleted)
{
    sw.Stop();

    Console.WriteLine($"线程执行完成,耗时:{Math.Round(Convert.ToDecimal(sw.ElapsedMilliseconds / 1000), 0)}秒");
}

Console.WriteLine("处理完成");
Console.ReadLine();